<!--头部公共部分-->
{include file="common/header" /}
<link rel="stylesheet" href="/admin/css/global.css" media="all">
<link rel="stylesheet" href="/static/font-awesome/css/font-awesome.min.css">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>设置</a>
        <a>权限管理</a>
        <a>
          <cite>权限分配</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div class="admin-main">
    <fieldset class="layui-elem-field">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>权限分配</legend>
        </fieldset>
        <div class="layui-field-box">
            <form class="layui-form">
                <div class="layui-form-item">
                    {foreach name="menus" item="vo" key="k"}
                    <div class="layui-collapse" lay-filter="test">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">{$vo.title}</h2>
                            <div class="layui-colla-content">
                                <div class="layui-input-block">
                                    <input type="checkbox" lay-skin="primary" name="menu[{$vo.id}]" value="{$vo['id']}" title="{$vo.title}" class="level_one" {if condition="in_array($vo['id'],$rulesArr)"}checked{/if}>
                                    {foreach name="vo[$vo['id']]" item="v"}
                                    <div class="layui-input-block">
                                        <input type="checkbox" lay-skin="primary" name="menu[{$v.id}]" title="{$v.title}" value="{$v['id']}" class="level_two" {if condition="in_array($v['id'],$rulesArr)"}checked{/if}>
                                        <div class="layui-input-block">
                                            {foreach name="v[$v['id']]" item="v1"}
                                            <input type="checkbox" lay-skin="primary" name="menu[{$v1.id}]" title="{$v1.title}" value="{$v1['id']}" class="level_three" {if condition="in_array($v1['id'],$rulesArr)"}checked{/if}>
                                            {/foreach}
                                        </div>
                                    </div>
                                    {/foreach}
                                </div>
                            </div>
                        </div>
                    </div>

                    {/foreach}
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="auth">立即提交</button>
                    </div>
                </div>
                <input type="hidden" name="id" value="{$id}">
            </form>
        </div>
    </fieldset>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    layui.use(['element', 'table', 'layer', 'laytpl','form'], function () {
        var $ = layui.$,
                element = layui.element,
                table = layui.table,
                layer = layui.layer,
                form = layui.form,
                laytpl = layui.laytpl;
        //选中
        $('.layui-form-checkbox').on('click', function (e) {
            var children = $(this).parent('.layui-input-block').find('.layui-form-checkbox');
            var input = $(this).parent('.layui-input-block').find('input');

            if ($(this).prev('input').hasClass('level_three')) {
                if ($(this).hasClass('layui-form-checked') == true) {
                    $(this).addClass('layui-form-checked')
                    $(this).prev('input').prop('checked', true);
                } else {
                    $(this).removeClass('layui-form-checked');
                    $(this).prev('input').prop('checked', false);
                }
            } else {
                if ($(this).hasClass('layui-form-checked') == true) {
                    children.addClass('layui-form-checked')
                    input.prop('checked', true);
                } else {
                    children.removeClass('layui-form-checked');
                    input.prop('checked', false);
                }

            }

        })
        //监听提交
        form.on('submit(auth)', function (data) {
            var menu_ids = data.field;
            var url = "{:url('Auth/rule_group')}";
            $.post(url, menu_ids, function (result) {
                if (result.code == 0) {
                    layer.msg(result.msg, {time: 2000});
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.location.reload();
                    }, 2000)
                }else {
                    layer.msg(result.msg, {time: 2000});
                }
            })
            return false;//阻止表单跳转
        });

    });
</script>